<?php defined('_VALID_MOS') or die('Direct Access to this location is not allowed.');

/*
 * Copyright Copyright (C) 2007 Alain Georgette. All rights reserved.
 * Copyright Copyright (C) 2006 Frantisek Hliva. All rights reserved.
 * License http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
 *
 * !JoomlaComment is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * !JoomlaComment is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA  02110-1301, USA.
 */

class josCommentSetting extends mosDBTable {
	/** @var int Primary key */
	var $id 				= 1;
	/** @var string */
	var $params				= null;

    function josCommentSetting(&$db)
    {
        $this->mosDBTable('#__comment_setting', 'id', $db);
    }

}

class config {
    var $_configFile;
    var $tabs;
    var $_complete_uninstall;
    var $_mambot_func;
    var $_ajax;
	var $_local_charset;
    var $_only_registered;
    var $_language;
    var $_moderator;
    var $_exclude_sections;
    var $_exclude_categories;
    var $_template;
    var $_template_modify;
    var $_emoticon_pack;
    var $_emoticon_wcount;
    var $_tree;
    var $_tree_indent;
    var $_sort_downward;
    var $_support_profiles;
    var $_support_avatars;
    var $_support_emoticons;
    var $_support_UBBcode;
    var $_support_pictures;
    var $_censorship_enable;
    var $_censorship_case_sensitive;
    var $_censorship_words;
    var $_censorship_usertypes;
    var $_IP_visible;
    var $_IP_partial;
    var $_IP_caption;
    var $_IP_usertypes;
    var $_preview_visible;
    var $_preview_length;
    var $_preview_lines;
    var $_voting_visible;
    var $_use_name;
    var $_notify_admin;
    var $_notify_email;
    var $_notify_users;
    var $_rss;
    var $_date_format;
    var $_captcha;
    var $_captcha_usertypes;
    var $_autopublish;
    var $_ban;
    var $_maxlength_text;
    var $_maxlength_word;
    var $_show_readon;
    var $_xmlerroralert;

    function config()
    {
        global $mosConfig_absolute_path;
//        $this->_configFile = "$mosConfig_absolute_path/components/com_comment/config.comment.php";
        $this->_configFile = "$mosConfig_absolute_path/administrator/components/com_comment/defaultconfig.comment.php";
    }
    
    function load()
    {
		global $database, $mosConfig_absolute_path;
		
        require($this->_configFile);
		/*
		 * october 2007 : store in database and not in file.
		 * to avoid many modifications with regression risk, this time 
		 * we keep the config logic of setting in variables
		 */
     	$row = new josCommentSetting($database);
    	$row->load(1);
   	    $params = new mosParameters( $row->params );
   	     
       	/* 
       	 * 'def' is doing 'get' with default value if not found. 
       	 */
        $this->_complete_uninstall 		= $params->def( '_complete_uninstall'	, $_complete_uninstall );
		$this->_mambot_func 			= $params->def( '_mambot_func'			, $_mambot_func );  
        $this->_ajax 					= $params->def( '_ajax'					, $_ajax );
        $this->_local_charset 			= $params->def( '_local_charset'		, $_local_charset );
        $this->check_local_charset();
        $this->_only_registered 		= $params->def( '_only_registered'		, $_only_registered );
        $this->_language 				= $params->def( '_language'				, $_language );
        $this->_moderator 				= $params->def( '_moderator'			, $_moderator);
        $this->_exclude_sections 		= $params->def( '_exclude_sections'		, $_exclude_sections);
        $this->_exclude_categories 		= $params->def( '_exclude_categories'	, $_exclude_categories);
        $this->_template 				= $params->def( '_template'				, $_template);
        $this->_template_modify			= $params->def( '_template_modify'		, $_template_modify);
        $this->_emoticon_pack 			= $params->def( '_emoticon_pack'		, $_emoticon_pack);
        $this->_emoticon_wcount 		= $params->def( '_emoticon_wcount'		, $_emoticon_wcount);
        $this->_tree 					= $params->def( '_tree'					, $_tree);
        $this->_tree_indent 			= $params->def( '_tree_indent'			, $_tree_indent);
        $this->_sort_downward 			= $params->def( '_sort_downward'		, $_sort_downward);
        $this->_support_profiles 		= $params->def( '_support_profiles'		, $_support_profiles);
		$this->_support_avatars 		= $params->def( '_support_avatars'		, $_support_avatars);
        $this->_support_emoticons 		= $params->def( '_support_emoticons'	, $_support_emoticons);
        $this->_support_UBBcode 		= $params->def( '_support_UBBcode'		, $_support_UBBcode);
        $this->_support_pictures 		= $params->def( '_support_pictures'		, $_support_pictures);
        $this->_censorship_enable 		= $params->def( '_censorship_enable'	, $_censorship_enable);
        $this->_censorship_case_sensitive = $params->def( '_censorship_case_sensitive'	, $_censorship_case_sensitive);
        $this->_censorship_words 		= $params->def( '_censorship_words'		, $_censorship_words);
        $this->_censorship_usertypes 	= $params->def( '_censorship_usertypes'	, $_censorship_usertypes);
        $this->_IP_visible 				= $params->def( '_IP_visible'			, $_IP_visible);
        $this->_IP_partial 				= $params->def( '_IP_partial'			, $_IP_partial);
        $this->_IP_caption 				= $params->def( '_IP_caption'			, $_IP_caption);
        $this->_IP_usertypes 			= $params->def( '_IP_usertypes'			, $_IP_usertypes);
        $this->_preview_visible 		= $params->def( '_preview_visible'		, $_preview_visible);
        $this->_preview_length 			= $params->def( '_preview_length'		, $_preview_length);
        $this->_preview_lines 			= $params->def( '_preview_lines'		, $_preview_lines);
        $this->_voting_visible 			= $params->def( '_voting_visible'		, $_voting_visible);
        $this->_use_name 				= $params->def( '_use_name'				, $_use_name);
        $this->_notify_admin 			= $params->def( '_notify_admin'			, $_notify_admin);
        $this->_notify_email 			= $params->def( '_notify_email'			, $_notify_email);
        $this->_notify_users 			= $params->def( '_notify_users'			, $_notify_users);
        $this->_rss 					= $params->def( '_rss'					, $_rss);
        $this->_date_format 			= $params->def( '_date_format'			, $_date_format);
        $this->_captcha 				= $params->def( '_captcha'				, $_captcha);
        $this->_captcha_usertypes 		= $params->def( '_captcha_usertypes'	, $_captcha_usertypes);
        $this->_autopublish 			= $params->def( '_autopublish'			, $_autopublish);
        $this->_ban 					= $params->def( '_ban'					, $_ban);
		$this->_maxlength_text 			= $params->def( '_maxlength_text'		, $_maxlength_text);
        $this->_maxlength_word 			= $params->def( '_maxlength_word'		, $_maxlength_word);
        $this->_show_readon 			= $params->def( '_show_readon'			, $_show_readon);
		/* technical default parameters */        
        $this->_xmlerroralert			= $params->def( '_xmlerroralert'		, $_getxmlerroralert);
    }
    
    function check_local_charset() {
        if (!$this->_local_charset) {
         	$iso = split( '=', _ISO );
        	$this->_local_charset = $iso[1];   
        }
    }
    function save($option="")
    {
        global $database;
            		
		/* simple paramters */            		
		$params = mosGetParam( $_POST, 'params', '' );
		/* arrays */
		$params['_moderator'] 				= implode( ',', JOSCGetArrayInts('_moderator'));
		$params['_exclude_sections'] 		= implode( ',', JOSCGetArrayInts('_exclude_sections') );
		$params['_exclude_categories'] 		= implode( ',', JOSCGetArrayInts('_exclude_categories') );
		$params['_IP_usertypes'] 			= implode( ',', JOSCGetArrayInts('_IP_usertypes') );
		$params['_captcha_usertypes'] 		= implode( ',', JOSCGetArrayInts('_captcha_usertypes') );
		$params['_censorship_usertypes'] 	= implode( ',', JOSCGetArrayInts('_censorship_usertypes') );

		if (is_array( $params )) {
			$txt = array();
			foreach ($params as $k=>$v) {
				$txt[] = "$k=$v";
			}
			$_POST['params'] = mosParameters::textareaHandling( $txt ); /* for the bind */
		}
		
    	$row = new josCommentSetting($database);
    	$row->load(1);
    	if (!$row->bind($_POST)) {
       		echo "<script> alert('" . $row->getError() . "'); window.history.go(-1); </script>\n";
        	exit();
    	}
	
    	if (!$row->store()) {
        	echo "<script> alert('" . $row->getError() . "'); window.history.go(-1); </script>\n";
        	exit();
   		}
//    	$row->updateOrder("contentid='$row->contentid'");

        /* Save TEMPLATE HTML AND CSS */
        if ($params['_template_modify']) {

            $templateHTML 		= strval( mosGetParam( $_POST, 'joscTemplateHTML', '' ) );
            $templateHTMLcontent 	= mosGetParam( $_POST, 'joscTemplateHTMLcontent', '', _MOS_ALLOWHTML );
	    $enable_writeHTML 	= mosGetParam($_POST,'enable_writeHTML',0);
	    $disable_writeHTML 	= mosGetParam($_POST,'disable_writeHTML',0);

            if ($templateHTML == $params['_template'] 	 && $templateHTMLcontent) {
                    $msg = "";
                    if (!$this->saveTemplateHTMLSource( $templateHTML, $templateHTMLcontent, $enable_writeHTML, $disable_writeHTML, $msg )) {
                        $msg = " Template not saved: " . $msg;
                    }
            }                


            $templateCSS		= strval( mosGetParam( $_POST, 'joscTemplateCSS', '' ) );
            $templateCSScontent 	= mosGetParam( $_POST, 'joscTemplateCSScontent', '', _MOS_ALLOWHTML );
	    $enable_writeCSS 	= mosGetParam($_POST,'enable_writeCSS',0);
	    $disable_writeCSS 	= mosGetParam($_POST,'disable_writeCSS',0);
    
            if ($templateCSS == $params['_template'] 	 && $templateCSScontent) {
                    $msg = "";
                    if (!$this->saveTemplateCSSSource( $templateCSS, $templateCSScontent, $enable_writeCSS, $disable_writeCSS, $msg )) {
                        $msg = " Template not saved: " . $msg;
                    }
            } 
        }               

        if ($option) {
        	mosRedirect("index2.php?option=$option&task=settings", 'Settings saved. '.$msg );
        } else return true;	
    }

    function saveTemplateHTMLSource( $template, $filecontent, $enable_write=0, $disable_write=0, &$msg ) {
	global $mosConfig_absolute_path;

//	$template 		= strval( mosGetParam( $_POST, 'template', '' ) );
//	$filecontent 	= mosGetParam( $_POST, 'filecontent', '', _MOS_ALLOWHTML );

	if ( !$template ) {
		$msg = '<b>Operation failed: No template specified.</b>';
		return false;
	}
	if ( !$filecontent ) {
		$msg = '<b>Operation failed: Content empty.</b>';
                return false;
	}
	$file = "$mosConfig_absolute_path/components/com_comment/joscomment/templates/". $template .'/index.html';

	$oldperms 	= fileperms($file);
	
	if ($enable_write) @chmod($file, $oldperms | 0222);

	clearstatcache(); /* ????????????????????? */
	if ( is_writable( $file ) == false ) {
		$msg = '<b>Operation failed: '. $file .' is not writable.</b>';
                return false;
	}

	if ( $fp = fopen ($file, 'w' ) ) {
		fputs( $fp, stripslashes( $filecontent ), strlen( $filecontent ) );
		fclose( $fp );

		if ($enable_write) {
			@chmod($file, $oldperms);
		} else {
			if ($disable_write)
				@chmod($file, $oldperms & 0777555);
		}
		return true;
	} else {
		if ($enable_write) @chmod($file, $oldperms);
		$msg = '<b>Operation failed: Failed to open file for writing.</b>';
		return false;
	}

    }
    
    function saveTemplateCSSSource( $template, $filecontent, $enable_write=0, $disable_write=0, &$msg ) {
	global $mosConfig_absolute_path;

//	$template 		= strval( mosGetParam( $_POST, 'template', '' ) );
//	$filecontent 	= mosGetParam( $_POST, 'filecontent', '', _MOS_ALLOWHTML );

	if ( !$template ) {
		$msg = '<b>Operation failed: No CSS specified.</b>';
		return false;
	}
	if ( !$filecontent ) {
		$msg = '<b>Operation failed: Content empty.</b>';
                return false;
	}
	$file = "$mosConfig_absolute_path/components/com_comment/joscomment/templates/". $template .'/css/template_css.css';

	$oldperms 	= fileperms($file);
	
	if ($enable_write) @chmod($file, $oldperms | 0222);

	clearstatcache(); /* clean PHP file cache */
	if ( is_writable( $file ) == false ) {
		$msg = '<b>Operation failed: '. $file .' is not writable.</b>';
                return false;
	}

	if ( $fp = fopen ($file, 'w' ) ) {
		fputs( $fp, stripslashes( $filecontent ), strlen( $filecontent ) );
		fclose( $fp );

		if ($enable_write) {
			@chmod($file, $oldperms);
		} else {
			if ($disable_write)
				@chmod($file, $oldperms & 0777555);
		}
		return true;
	} else {
		if ($enable_write) @chmod($file, $oldperms);
		$msg = '<b>Operation failed: Failed to open file for writing.</b>';
		return false;
	}

    }

    function generalPage()
    {
        global $mosConfig_absolute_path;
        $this->tabs->startTab("General", "General-page");
        $rows = new tabRows();

        $rows->addTitle("Basic Settings");
        $row = new tabRow();
        $row->caption = 'Uninstall complete mode:';
        $row->component = mosHTML::yesnoRadioList('params[_complete_uninstall]', 'class="inputbox"', $this->_complete_uninstall);
        $row->help = 'Delete also tables when uninstall ! Do not active except if you would not use !joomlacomment anymore.';
        $rows->addRow($row);
        $row = new tabRow();
        $row->caption = 'Mambot content function:';
        $row->component = input('params[_mambot_func]', 'class="inputbox"', $this->_mambot_func);
        $row->help = '<u>For expert only !</u> You can change here the joscomment mambot function, if you have <b>hacked</b> the content html (for example: to display the read only function first).';
        $rows->addRow($row);
        $row = new tabRow();
        $row->caption = 'Ajax support (recommended):';
        $row->component = mosHTML::yesnoRadioList('params[_ajax]', 'class="inputbox"', $this->_ajax);
        $row->help = 'Asynchronous JavaScript + XML';
        $rows->addRow($row);
        $row = new tabRow();
        $row->caption = 'Language:';
        $row->component = mosHTML::selectList(languageList("$mosConfig_absolute_path/components/com_comment/joscomment/language/"), 'params[_language]', 'class="inputbox"', 'value', 'text', $this->_language);
        $row->help = '';
        $rows->addRow($row);
        $row = new tabRow();
        $row->caption = 'Local charset :<br />if you are upgrading older release thant 3.0.0, read carefully description on the right !';
        $row->component = input('params[_local_charset]', 'class="inputbox"', $this->_local_charset);
        if (!function_exists("iconv"))
        	$row->help 	.= 'Will not be used !! php <a href="http://www.php.net/manual/fr/ref.iconv.php" target="_blank">iconv library</a><u/> is not available.  <b>If you are using a NOT utf-8 and a NOT iso-8859-1 joomla installation, please contact your administrator OR deactivate the ajax support parameter.</b>';
		else        	
	 		$row->help 	.= 'Php <a href="http://www.php.net/manual/fr/ref.iconv.php" target="_blank">iconv library</a> is available on your server.'
	                    .  '<br /><b>Input the charset of your joomla installation if it is different from utf-8.<br />Clic <a href="http://www.gnu.org/software/libiconv/" target="_blank">HERE</a> to check if it is supported by the inconv library ! else, contact the joomlacomment support.</b> '
						.  '<br /><br /><b>If this is an upgrade from a release older than 3.0.0</b>, once you have saved this parameter, <u>go to Manage Comments and use the Convert To LCharset</u> functionnality to convert related comments.'
        				.  '<br />If you have always used the ajax mode, convert every comments.'
        				.  '<br />If you had changed, some comments (created with ajax) have to be converted, some others (created without ajax) have not to be converted !'
        				.  ' In this case, select concerned comments only (those with strange characters !).'
        				;
        $rows->addRow($row);        

        $rows->addTitle("Sections & Categories");
		$row = new tabRow();
        $row->caption = 'Exclude sections:';
        $row->component = sections('_exclude_sections', $this->_exclude_sections);
        $row->help = '';
		$rows->addRow($row);
        $row = new tabRow();
        $row->caption = 'Exclude categories:';
        $row->component = categories('_exclude_categories', $this->_exclude_categories);
        $row->help = '';
        $rows->addRow($row);

        $rows->addTitle("Notification");
        $row = new tabRow();
        $row->caption = 'Notify admin:';
        $row->component = mosHTML::yesnoRadioList('params[_notify_admin]', 'class="inputbox"', $this->_notify_admin);
        $row->help = 'Notify an administrator by email?';
        $rows->addRow($row);
        $row = new tabRow();
        $row->caption = 'Admin\'s email:';
        $row->component = input('params[_notify_email]', 'class="inputbox"', $this->_notify_email);
        $row->help = 'Mail notificationto which email address?';
        $rows->addRow($row);
        $row = new tabRow();
        $row->caption = 'Enable users notification :';
        $row->component = mosHTML::yesnoRadioList('params[_notify_users]', 'class="inputbox"', $this->_notify_users);
        $row->help = 'Will be used only if Autopublish comments is set.';
        $rows->addRow($row);
        $row->caption = 'Enable comment feed (RSS):';
        $row->component = mosHTML::yesnoRadioList('params[_rss]', 'class="inputbox"', $this->_rss);
        $row->help = '';        
        $rows->addRow($row);

        $rows->addTitle("Technical parameters (for joomlacomment support only)");
        $row = new tabRow();
        $row->caption = 'xmlErrorAlert:';
        $row->component = mosHTML::yesnoRadioList('params[_xmlerroralert]', 'class="inputbox"', $this->_xmlerroralert);
        $row->help = '';
        $rows->addRow($row);

        echo $rows->tabRows_htmlCode();
        $this->tabs->endTab();
    }
    function layoutPage()
    {
        global $mosConfig_absolute_path;
        $this->tabs->startTab("Layout", "Layout-page");
        $rows = new tabRows();

        $rows->addTitle("Basic settings");
        $row = new tabRow();
        $row->caption = 'Template:';
        $row->component = mosHTML::selectList(folderList("$mosConfig_absolute_path/components/com_comment/joscomment/templates"), 'params[_template]', 'class="inputbox" size="5"', 'value', 'text', $this->_template);
        $row->help = 'Templates are the different looks of your comments.'
				   . '<br />If you have enabled emoticons, adapt the numbers of emoticons by line (see below) according to your template choice.';
        $rows->addRow($row);
        $row = new tabRow();
        $row->caption = 'Template modify:';
        $row->component = mosHTML::yesnoRadioList( 'params[_template_modify]', 'class="inputbox" ', $this->_template_modify);
        $row->help = 'Set YES if you would like to modify the current joomlacomment template HTML and CSS style. 2 new tabs will appear after saving the setting.'
                   .  '<br />Until new template features, Be aware that during upgrade it <b>will be overwritten</b> => <b>think to keep safe in external files your modifications before upgrade.</b>';
        $rows->addRow($row);
        
        $rows->addTitle("Emoticons");
        $row = new tabRow();
        $row->caption = 'Emoticon (smilies) support:';
        $row->component = mosHTML::yesnoRadioList('params[_support_emoticons]', 'class="inputbox"', $this->_support_emoticons);
        $row->help = 'Allow the use of Emoticons in comments ?';
        $rows->addRow($row);        
        $row = new tabRow();
        $row->caption = 'Emoticon pack:';
        $row->component = mosHTML::selectList(folderList("$mosConfig_absolute_path/components/com_comment/joscomment/emoticons"), 'params[_emoticon_pack]', 'class="inputbox" size="3"', 'value', 'text', $this->_emoticon_pack);
        $row->help =  $this->emoticons_confightml();
        $rows->addRow($row);
        $row = new tabRow();
        $row->caption = 'Number of emoticon by line:';
        $row->component = input('params[_emoticon_wcount]', 'class="inputbox"', $this->_emoticon_wcount);
        $row->help = 'Number of emoticon to display on each line. Value 0 means no limit.'
        			. '<br />Proposition: use 12 in case of <i>emotop</i> templates (emoticon are on top) and 2 or 3 if possible for others (left emoticon templates). Try and see what is the best for your website !' 
        			;
        $rows->addRow($row);

        $rows->addTitle("Structure");
        $row = new tabRow();
        $row->caption = 'Allow nested comments:';
        $row->component = mosHTML::yesnoRadioList('params[_tree]', 'class="inputbox"', $this->_tree);
        $row->help = 'This will allow users to insert post in response to any post of the content item (not only the last), with an indent display.';
        $rows->addRow($row);
        $row = new tabRow();
        $row->caption = 'Indent (pixels):';
        $row->component = input('params[_tree_indent]', 'class="inputbox"', $this->_tree_indent);
        $row->help = 'This is used to indent messages in threaded view.';
        $rows->addRow($row);
        $row = new tabRow();
        $row->caption = 'Comments sorting (if NOT nested comments):';
        $sorting = array();
        $sorting[] = mosHTML::makeOption('1', 'New entries first');
        $sorting[] = mosHTML::makeOption('0', 'New entries last');
        //$rowSorting->id = 'sorting';
        $row->component = mosHTML::selectList($sorting, 'params[_sort_downward]', 'class="inputbox"', 'value', 'text', $this->_sort_downward);
        $row->help = 'Ordering of comments : used only if nested is NOT active.<br /> If New entries first, the form will be on the top, else it will be on the bottom';
        $rows->addRow($row);

        $rows->addTitle("Posting");
        $row = new tabRow();
        $row->caption = 'UBB code support:';
        $row->component = mosHTML::yesnoRadioList('params[_support_UBBcode]', 'class="inputbox"', $this->_support_UBBcode);
        $row->help = 'Allow the use of UBB Codes?';
        $rows->addRow($row);
        $row = new tabRow();
        $row->caption = 'Picture support:';
        $row->component = mosHTML::yesnoRadioList('params[_support_pictures]', 'class="inputbox"', $this->_support_pictures);
        $row->help = 'Allow the use of pictures in comments?';
        $rows->addRow($row);
        $row = new tabRow();
        $row->caption = 'Enable voting:';
        $row->component = mosHTML::yesnoRadioList('params[_voting_visible]', 'class="inputbox"', $this->_voting_visible);
        $row->help = 'If set AND ajax mode is set : will display reactive image to allow to vote + or - for any comments.';
        $rows->addRow($row);
        $row = new tabRow();
        $row->caption = 'Use names :';
        $row->component = mosHTML::yesnoRadioList('params[_use_name]', 'class="inputbox"', $this->_use_name);
        $row->help = "Use names rather than usernames (it concerns only registered users)";
		$rows->addRow($row);
        $row = new tabRow();
        $row->caption = 'Enable profiles:';
        $row->component = mosHTML::yesnoRadioList('params[_support_profiles]', 'class="inputbox"', $this->_support_profiles);
        $row->help = "Allow the use of <a href='http://www.joomlapolis.com/' target='_blank'>Community Builder</a> - Profiles in comments ?";
		$rows->addRow($row);
        $row = new tabRow();
        $row->caption = 'Enable avatars:';
        $row->component = mosHTML::yesnoRadioList('params[_support_avatars]', 'class="inputbox"', $this->_support_avatars);
        $row->help = "Allow the use of <a href='http://www.joomlapolis.com/' target='_blank'>Community Builder</a> - Avatars in comments ? (only if profiles is enabled)";
		$rows->addRow($row);
        $row = new tabRow();
        $row->caption = 'Date format:';
        $row->component = input('params[_date_format]', 'class="inputbox"', $this->_date_format);
        $row->help = 'The syntax used is identical to the PHP date() function.';
        $rows->addRow($row);
        
        $rows->addTitle("IP Adress");
        $row = new tabRow();
        $row->caption = 'Visible:';
        $row->component = mosHTML::yesnoRadioList('params[_IP_visible]', 'class="inputbox"', $this->_IP_visible);
        $row->help = 'If set, will display the IP address of the writer.';
        $rows->addRow($row);
        $row = new tabRow();
        $row->caption = 'Partial:';
        $row->component = mosHTML::yesnoRadioList('params[_IP_partial]', 'class="inputbox"', $this->_IP_partial);
        $row->help = 'If set, and IP is visible, will not display the last digit of the IP address';
        $rows->addRow($row);
        $row = new tabRow();
        $row->caption = 'Caption:';
        $row->component = input('params[_IP_caption]', 'class="inputbox"', $this->_IP_caption);
        $row->help = 'Description before display the IP value';
        $rows->addRow($row);
        $row = new tabRow();
        $row->caption = 'Usertypes:';
        $row->component = usertypes('_IP_usertypes', $this->_IP_usertypes);
        $row->help = 'Will be applied only to the selected usertypes.';
        $rows->addRow($row);
        
        $rows->addTitle("Frontpage");
        $row = new tabRow();
        $row->caption = 'Show "Read On":';
        $row->component = mosHTML::yesnoRadioList('params[_show_readon]', 'class="inputbox"', $this->_show_readon);
        $row->help = 'In case of Content Item Intro display (as on frontpage), will display a write comment link with the count of existing comments for this content item';
        $rows->addRow($row);
        $row = new tabRow();
        $row->caption = 'Preview visible:';
        $row->component = mosHTML::yesnoRadioList('params[_preview_visible]', 'class="inputbox"', $this->_preview_visible);
        $row->help = 'Preview of comments (if Read On is enable)';
        $rows->addRow($row);
        $row = new tabRow();
        $row->caption = 'Preview Length:';
        $row->component = input('params[_preview_length]', 'class="inputbox"', $this->_preview_length);
        $row->help = '';
        $rows->addRow($row);
        $row = new tabRow();
        $row->caption = 'Preview Number of lines:';
        $row->component = input('params[_preview_lines]', 'class="inputbox"', $this->_preview_lines);
        $row->help = '';
        $rows->addRow($row);


        echo $rows->tabRows_htmlCode();
        $this->tabs->endTab();

/* 
        $rowSorting->visible(!$this->_tree);
		$element = element::get('sorting');
		
        onClick('_tree0', $element . element::visible(true));
        onClick('_tree1', $element . element::visible(false));
*/
    }

    function emoticons_confightml() {
        global $mosConfig_absolute_path, $mosConfig_live_site;
       
        $_emoticons_path = $mosConfig_live_site."/components/com_comment/joscomment/emoticons/$this->_emoticon_pack/images";
        require_once("$mosConfig_absolute_path/components/com_comment/joscomment/emoticons/$this->_emoticon_pack/index.php");
		require_once("$mosConfig_absolute_path/components/com_comment/joscomment/comment.class.php");
        $form = new form(null);
        $form->setSupport_emoticons($this->_support_emoticons);
        $form->setEmoticons($this->_emoticons);
        $form->setEmoticons_path($_emoticons_path);
        $form->setEmoticonWCount($this->_emoticon_wcount);
        return $form->emoticons(false);
    }
    
    function securityPage()
    {
        $this->tabs->startTab("Security", "Security-page");
        $rows = new tabRows();
        
        $rows->addTitle("Basic Settings");
        $row = new tabRow();        
        $row->caption = 'Only registered:';
        $row->component = mosHTML::yesnoRadioList('params[_only_registered]', 'class="inputbox"', $this->_only_registered);
        $row->help = 'Only registered users can write comments.';
        $rows->addRow($row);
        $row = new tabRow();
        $row->caption = 'Autopublish comments:';
        $row->component = mosHTML::yesnoRadioList('params[_autopublish]', 'class="inputbox"', $this->_autopublish);
        $row->help = 'If you set this to "no" then comments will be added to the database and will wait for you to review and publish them before showing.';
        $rows->addRow($row);
        $row = new tabRow();
        $row->caption = 'Banlist:';
        $row->component = textarea('params[_ban]', 'class="inputbox" rows="5"', $this->_ban);
        $row->help = 'To specify several different IP addresses separate them with commas.';
        $rows->addRow($row);
        $row = new tabRow();
        $row->caption = 'Moderator groups:';
        $row->component = usertypes('_moderator', $this->_moderator, false);
        $row->help = 'Moderators will be able to modify or delete on-line any comments. A special menu will appear on each comment for those users.';
        $rows->addRow($row);
        
        $rows->addTitle("Overflow");
        $row = new tabRow();
        $row->caption = 'Post max length:';
        $row->component = input('params[_maxlength_text]', 'class="inputbox"', $this->_maxlength_text);
        $row->help = 'Maximum allowed characters in posts (-1 for no max.)';
        $rows->addRow($row);
        $row = new tabRow();
        $row->caption = 'Word max length:';
        $row->component = input('params[_maxlength_word]', 'class="inputbox"', $this->_maxlength_word);
        $row->help = 'Maximum allowed characters in words (-1 for no max.)';
        $rows->addRow($row);
        
        $rows->addTitle("Captcha");
        $row = new tabRow();
        $row->caption = 'Enable (recommended):';
        $row->component = mosHTML::yesnoRadioList('params[_captcha]', 'class="inputbox"', $this->_captcha);
        $row->help = 'Will force the user to input a random string displayed in a specially crafted image. This will disallow automated submits to your site.';
        $rows->addRow($row);
        $row = new tabRow();
        $row->caption = 'Usertypes:';
        $row->component = usertypes('_captcha_usertypes', $this->_captcha_usertypes);
        $row->help = 'Only the selected usertypes will have to.';
        $rows->addRow($row);
        
        $rows->addTitle("Censorship");
        $row = new tabRow();
        $row->caption = 'Enable:';
        $row->component = mosHTML::yesnoRadioList('params[_censorship_enable]', 'class="inputbox"', $this->_censorship_enable);
        $row->help = 'Active or not censor filter. The censor will use the rules written in the censored words list to hide or change those words';
        $rows->addRow($row);
        $row = new tabRow();
        $row->caption = 'Case sensitive:';
        $row->component = mosHTML::yesnoRadioList('params[_censorship_case_sensitive]', 'class="inputbox"', $this->_censorship_case_sensitive);
        $row->help = '';
        $rows->addRow($row);
        $row = new tabRow();
        $row->caption = 'Censored words:';
        $row->component = textarea('params[_censorship_words]', 'class="inputbox" rows="5" cols="70"', $this->_censorship_words);
        $row->help = false;
        $rows->addRow($row);
        $row = new tabRow();
        $row->caption = 'Usertypes:';
        $row->component = usertypes('_censorship_usertypes', $this->_censorship_usertypes);
        $row->help = 'Will be applied only to the selected usertypes.';
        $rows->addRow($row);
        
        echo $rows->tabRows_htmlCode();
		$this->tabs->endTab();
    }

    function editTemplateHTMLPage() {
        $this->tabs->startTab("HTML", "TemplateHTML");
        $this->editTemplateHTMLSource();
		$this->tabs->endTab();
    }

    function editTemplateCSSPage() {
        $this->tabs->startTab("CSS", "TemplateCSS");
        $this->editTemplateCSSSource();
		$this->tabs->endTab();
    }

    function execute($option)
    {
?>
 		You have questions, problems or features requests ? contact the <a href="http://joomlacode.org/gf/project/joomagecomment/" target="_blank">joomlacomment support</a>!
        <form action='index2.php' method='POST' name='adminForm'>
<?php
        $this->tabs = new mosTabs(1); /* 1 to remember the selected tab */
        $this->tabs->startPane("jos_comment");
        $this->generalPage();
        $this->securityPage();
        $this->layoutPage();
        if ($this->_template_modify) {
          $this->editTemplateHTMLPage();
          $this->editTemplateCSSPage();
        }
        $this->tabs->endPane();
?>     
		<input type="hidden" name="task" value="" />   
		<input type="hidden" name="option" value="<?php echo $option; ?>" />   
        </form>
<?php
    }

    function editTemplateHTMLSource() {
	global $mosConfig_absolute_path;

	$file = "$mosConfig_absolute_path/components/com_comment/joscomment/templates/". $this->_template ."/index.html";

	if ( $fp = fopen( $file, 'r' ) ) {
		$content = fread( $fp, filesize( $file ) );
		$content = htmlspecialchars( $content );

		$this->HTML_editTemplateHTMLSource( $content, $file );
	} else {
		echo "<b>Operation Failed: Could not open". $file . "</b>";
                return;
	}
    }

    function editTemplateCSSSource() {
	global $mosConfig_absolute_path;

	$file = "$mosConfig_absolute_path/components/com_comment/joscomment/templates/". $this->_template ."/css/template_css.css";

	if ( $fp = fopen( $file, 'r' ) ) {
		$content = fread( $fp, filesize( $file ) );
		$content = htmlspecialchars( $content );

		$this->HTML_editTemplateCSSSource( $content, $file );
	} else {
		echo "<b>Operation Failed: Could not open". $file . "</b>";
                return;
	}
    }

	function HTML_editTemplateHTMLSource( &$content, $file ) {
		global $mosConfig_absolute_path;
		$template_path = $file;
                $template = $this->_template;
		?>
		<table cellpadding="1" cellspacing="1" border="0" width="100%">
		<tr>
			<td><table class="adminheading"><tr>
			  <th class="templates">HTML template editor : <?php echo $template; ?></th>
			</tr></table></td>
			<td>
				<span class="componentheading">index.html is :
				<b><?php echo is_writable($template_path) ? '<font color="green"> Writable</font>' : '<font color="red"> Not Writable</font>'; ?></b>
				</span>
			</td>
<?php
			if (mosIsChmodable($template_path)) {
				if (is_writable($template_path)) {
?>
		  <td>
				<input type="checkbox" id="disable_writeHTML" name="disable_writeHTML" value="1"/>
				<label for="disable_writeHTML"></label>
				Set Not Writable
				<label for="label">after saved</label></td>
<?php
				} else {
?>
		  <td>
				<input type="checkbox" id="enable_writeHTML" name="enable_writeHTML" value="1"/>
			  <label for="enable_writeHTML">Ignore the Writable / Not Writable status</label>			</td>
<?php
				} // if
			} // if
?>
		</tr>
		</table>
		<table class="adminform">
			<tr><th><?php echo $template_path; ?></th></tr>
			<tr><td><textarea style="width:100%;height:500px" cols="110" rows="25" name="joscTemplateHTMLcontent" class="inputbox"><?php echo $content; ?></textarea></td></tr>
		</table>
		<input type="hidden" name="joscTemplateHTML" value="<?php echo $template; ?>" />
		<?php
	}

	function HTML_editTemplateCSSSource( &$content, $file ) {
		global $mosConfig_absolute_path;
		$template_path = $file;
                $template = $this->_template;
		?>
		<table cellpadding="1" cellspacing="1" border="0" width="100%">
		<tr>
			<td><table class="adminheading"><tr>
			  <th class="templates">CSS template editor : <?php echo $template; ?></th>
			</tr></table></td>
			<td>
				<span class="componentheading">index.html is :
				<b><?php echo is_writable($template_path) ? '<font color="green"> Writable</font>' : '<font color="red"> Not Writable</font>'; ?></b>
				</span>
			</td>
<?php
			if (mosIsChmodable($template_path)) {
				if (is_writable($template_path)) {
?>
		  <td>
				<input type="checkbox" id="disable_writeCSS" name="disable_writeCSS" value="1"/>
				<label for="disable_writeCSS"></label>
				Set Not Writable
				<label for="label">after saved</label></td>
<?php
				} else {
?>
		  <td>
				<input type="checkbox" id="enable_writeCSS" name="enable_writeCSS" value="1"/>
			  <label for="enable_writeCSS">Ignore the Writable / Not Writable status</label>			</td>
<?php
				} // if
			} // if
?>
		</tr>
		</table>
		<table class="adminform">
			<tr><th><?php echo $template_path; ?></th></tr>
			<tr><td><textarea style="width:100%;height:500px" cols="110" rows="25" name="joscTemplateCSScontent" class="inputbox"><?php echo $content; ?></textarea></td></tr>
		</table>
		<input type="hidden" name="joscTemplateCSS" value="<?php echo $template; ?>" />
		<?php
	}

}

?>